package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author 
	 */
	public class Main extends Sprite 
	{
		
		private var map:Map;
		private var drawPath:Sprite;
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			stage.scaleMode = StageScaleMode.NO_SCALE;
			map = new Map();
			addChild(map);
			drawPath = new Sprite();
			map.addChild(drawPath);
			drawPath.graphics.lineStyle(0);
			drawPath.graphics.beginFill(0xff0000);
			drawPath.graphics.drawRect(Map.TILE_SIZE * map.startNode.col, Map.TILE_SIZE * map.startNode.row
			,Map.TILE_SIZE,Map.TILE_SIZE);
			map.drawMap();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, flushMap);
			map.addEventListener(MouseEvent.CLICK, clickGrid);
		}
		private function flushMap(evt:KeyboardEvent):void
		{
			if (evt.keyCode == 32)
			{
				map.removeEventListener(MouseEvent.CLICK,clickGrid);
				removeChild(map);
				map = new Map();
				map.addEventListener(MouseEvent.CLICK,clickGrid);
				map.drawMap();
				addChild(map);
			}
		}
		private function clickGrid(evt:MouseEvent):void
		{	
			drawPath.graphics.clear();
			var clickCol:uint = Math.floor(mouseX / Map.TILE_SIZE);
			var clickRow:uint = Math.floor(mouseY / Map.TILE_SIZE);
			//trace(clickRow,clickCol);
			var tempNode:Node = map.getNode(clickRow, clickCol);
			if (tempNode.isWalkAble == false)
			{
				trace("不可通过");
			}
			else
			{
				map.endNode = map.getNode(clickRow, clickCol);	
				//map.drawMap();
				if (map.endNode != null)
				{
					var astar:Astar = new Astar(map);
					var timer:int = getTimer();
					var isFind:Boolean = astar.findPath();
					trace("寻路用时："+(getTimer()-timer));
					if (isFind)
					{
						var path:Array = astar.buildPath();
						for (var i:uint = 0; i < path.length; i++ )
						{
							if (path[i] == map.startNode)
							{
								drawPath.graphics.lineStyle(0);
								drawPath.graphics.beginFill(0xff0000);
								drawPath.graphics.drawRect(path[i].col * Map.TILE_SIZE, path[i].row * Map.TILE_SIZE
									,Map.TILE_SIZE, Map.TILE_SIZE);
								drawPath.graphics.endFill();
							}
							else if (path[i] == map.endNode)
							{
								drawPath.graphics.lineStyle(0);
								drawPath.graphics.beginFill(0x0000ff);
								drawPath.graphics.drawRect(path[i].col * Map.TILE_SIZE, path[i].row * Map.TILE_SIZE
									,Map.TILE_SIZE, Map.TILE_SIZE);
								drawPath.graphics.endFill();
							}
							else
							{
								drawPath.graphics.lineStyle(0);
								drawPath.graphics.beginFill(0x660033);
								drawPath.graphics.drawRect(path[i].col * Map.TILE_SIZE, path[i].row * Map.TILE_SIZE
									,Map.TILE_SIZE, Map.TILE_SIZE);
								drawPath.graphics.endFill();
							}
						}
						map.startNode = map.endNode;
					}
				}
			}
		}
		
	}
	
}